#define _CRT_SECURE_NO_WARNINGS 1

#include "Common.h"
#include "ConcurrentAlloc.h"

void Alloc1()
{
	for (size_t i = 0; i < 5; ++i)
	{
		void* ptr = ConcurrentAlloc(6);
	}
}

void Alloc2()
{
	for (size_t i = 0; i < 5; ++i)
	{
		void* ptr = ConcurrentAlloc(7);
	}
}


void TLSTest()
{
	std::thread t1(Alloc1);
	t1.join();

	std::thread t2(Alloc2);
	t2.join();
}

void TestConcurrentAlloc1()
{
	void* ptr1 = ConcurrentAlloc(6);
	void* ptr2 = ConcurrentAlloc(8);
	void* ptr3 = ConcurrentAlloc(7);
	void* ptr4 = ConcurrentAlloc(8);
	void* ptr5 = ConcurrentAlloc(8);
	void* ptr6 = ConcurrentAlloc(8);
	void* ptr7 = ConcurrentAlloc(8);


	cout << ptr1 << endl;
	cout << ptr2 << endl;
	cout << ptr3 << endl;
	cout << ptr4 << endl;
	cout << ptr5 << endl;

	ConcurrentFree(ptr1);
	ConcurrentFree(ptr2);
	ConcurrentFree(ptr3);
	ConcurrentFree(ptr4);
	ConcurrentFree(ptr5);
	ConcurrentFree(ptr6);
	ConcurrentFree(ptr7);

}

void TestConcurrentAlloc2()
{
	void* ptr = nullptr;
	for (int i = 0; i < 1024; i++)
	{
		ptr = ConcurrentAlloc(6);
	}

	void* ptr1= ConcurrentAlloc(6);
}

void MultiAlloc1()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; ++i)
	{
		void* ptr = ConcurrentAlloc(6);
		v.push_back(ptr);
	}

	for (auto p : v)
	{
		ConcurrentFree(p);
	}
}

void MultiAlloc2()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; ++i)
	{
		void* ptr = ConcurrentAlloc(16);
		v.push_back(ptr);
	}

	for (auto p : v)
	{
		ConcurrentFree(p);
	}
}

void TestMultiProcess()
{
	std::thread t1(MultiAlloc1);
	std::thread t2(MultiAlloc2);

	t1.join();
	t2.join();
}

void BigTest()
{
	void* ptr1 = ConcurrentAlloc(257 * 1024);
	ConcurrentFree(ptr1);

	void* ptr2 = ConcurrentAlloc(129 * 8 * 1024);
	ConcurrentFree(ptr2);
}

//int main()
//{
//	//TLSTest();
//	//TestConcurrentAlloc1();
//	//TestMultiProcess();
//	BigTest();
//}